﻿namespace EasyWeb
{
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using EasyWeb.Data;
    using EasyWeb.Design;
    using EasyWeb.Validation;

    [EWDisplayName(Name = "FieldDefinitionMultiLineText", ResourceType = typeof(EWFieldDefinitionResources))]
    public class EWFieldDefinitionMultiLineText : EWFieldDefinition
    {
        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowDefaultValue")]
        public override bool AllowDefaultValue
        {
            get { return true; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_AllowUnique")]
        public override bool AllowUnique
        {
            get { return false; }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinition_DefaultValue")]
        public override object DefaultValue
        {
            get { return this.StateBag.Get<string>("DefaultValue"); }
            set { this.StateBag.Set("DefaultValue", (string)value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionMultiLineText_NumberOfLines")]
        [EWValueCompareValidation(0, EWCompareMode.GreaterThan)]
        public int NumberOfLines
        {
            get { return this.StateBag.Get<int>("NumberOfLines"); }
            set { this.StateBag.Set("NumberOfLines", value); }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionMultiLineText_OperationMode")]
        public EWTextOperationMode OperationMode
        {
            get { return this.StateBag.Get<EWTextOperationMode>("OperationMode", () => EWTextOperationMode.Replace); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("OperationMode", value);
            }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionMultiLineText_StoreMode")]
        public EWTextStoreMode StoreMode
        {
            get { return this.StateBag.Get<EWTextStoreMode>("StoreMode", () => EWTextStoreMode.Unicode); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("StoreMode", value);
            }
        }

        [Display(ResourceType = typeof(EWFieldDefinitionResources), Name = "FieldDefinitionMultiLineText_TextMode")]
        public EWTextMode TextMode
        {
            get { return this.StateBag.Get<EWTextMode>("TextMode", () => EWTextMode.Plain); }
            set
            {
                EWGuard.IsDefinedEnum(value, "value");
                this.StateBag.Set("TextMode", value);
            }
        }

        public override EWFieldDefinitionColumnCollection CreateFieldDefinitionColumns()
        {
            return new EWFieldDefinitionColumnCollection()
            {
                new EWFieldDefinitionColumn()
                {
                    Name = this.InternalName,
                    IsNullable = this.AllowNull,
                    IsUnique = this.AllowUnique && this.IsUnique,
                    Type = DbType.String,
                    Length = int.MaxValue
                }
            };
        }

        public override void Deserialize(EWSerializationInfo info)
        {
            base.Deserialize(info);

            this.NumberOfLines = info.GetInt32("NumberOfLines");
            this.OperationMode = info.GetEnum<EWTextOperationMode>("OperationMode");
            this.StoreMode = info.GetEnum<EWTextStoreMode>("StoreMode");
            this.TextMode = info.GetEnum<EWTextMode>("TextMode");
            this.DefaultValue = info.GetString("DefaultValue");
        }

        public override void Serialize(EWSerializationInfo info)
        {
            base.Serialize(info);

            info.AddValue("NumberOfLines", this.NumberOfLines);
            info.AddValue("OperationMode", this.OperationMode);
            info.AddValue("StoreMode", this.StoreMode);
            info.AddValue("TextMode", this.TextMode);
            info.AddValue("DefaultValue", (string)this.DefaultValue);
        }
    }
}
